package com.xiao.seckil.service.impl;

import com.xiao.group.common.Vo.UsersEntity;
import com.xiao.group.common.utils.R;
import com.xiao.seckil.redis.Miaoshakey;
import com.xiao.seckil.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.Query;

import com.xiao.seckil.dao.SeckillDao;
import com.xiao.seckil.entity.SeckillEntity;
import com.xiao.seckil.service.SeckillService;


@Service("seckillService")
public class SeckillServiceImpl extends ServiceImpl<SeckillDao, SeckillEntity> implements SeckillService {

    @Autowired
    RedisService redisService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SeckillEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("seckil_status",1);
        IPage<SeckillEntity> page = this.page(
                new Query<SeckillEntity>().getPage(params),
              wrapper
        );

        return new PageUtils(page);
    }

    //添加订单活动表
    @Override
    public R tosave(SeckillEntity seckill) {
        seckill.setSeckilStatus(1);
        seckill.setSeckilCreateAt(new Date());
        seckill.setSeckilRemind(1);
        SeckillEntity seckillEntity = baseMapper.selectOne(new QueryWrapper<SeckillEntity>().eq("goods_id", seckill.getGoodsId()).ne("seckil_status", 0));
        if(seckillEntity ==null){
            int insert = baseMapper.insert(seckill);
            return R.ok().put("data",insert);
        }
        return R.error();
    }

    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public String creatPath(UsersEntity usersEntity, Integer goodsId) {
        //uuid 中的 ' - '替换掉
        String path = UUID.randomUUID().toString().replace("-", "");
        redisService.set(Miaoshakey.getMiaoshaPathKey,""+usersEntity.getId()+"_"+goodsId,path);
        return path;
    }

}